package com.cloudspace.yunloan.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cloudspace.yunloan.R;
import com.cloudspace.yunloan.constants.Constants;
import com.cloudspace.yunloan.entity.LoginResult;
import com.cloudspace.yunloan.network.JsonCallBack;
import com.cloudspace.yunloan.network.api.API;
import com.cloudspace.yunloan.utils.AppPreferences;
import com.cloudspace.yunloan.utils.ToastMaker;

import okhttp3.Call;
import okhttp3.Response;

public class LoginActivity extends YunloanActivity {
    private EditText loginNameEt;
    private EditText loginPasswordEt;

    private TextView loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login, false);

        loginNameEt = findViewById(R.id.login_name_et);
        loginPasswordEt = findViewById(R.id.login_password_et);
        loginButton = findViewById(R.id.login);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });

        if (!TextUtils.isEmpty(AppPreferences.getString(Constants.USER_ID))){
            loginNameEt.setText(AppPreferences.getString(Constants.USER_ID));
            loginPasswordEt.requestFocus();
        }
    }

    private void login() {
        final String name = loginNameEt.getText().toString();
        String password = loginPasswordEt.getText().toString();

        if (TextUtils.isEmpty(name)) {
            ToastMaker.showToastLong(R.string.login_name_hint);
            return;
        }

        if (name.length() < 3 || name.length() > 10) {
            ToastMaker.showToastShort("用户名长度需要在3-10之间");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastMaker.showToastLong(R.string.login_password_hint);
            return;
        }

        if (password.length() < 4 || password.length() > 20) {
            ToastMaker.showToastShort("密码长度需要在4-20之间");
            return;
        }

        showLoadingDialog("正在登录...");

        API.getInstance().getUserApi().login(this, name, password, new JsonCallBack<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult, Call call, Response response) {
                hideLoadingDialog();

                if (loginResult.data != null) {
                    if (TextUtils.isEmpty(loginResult.data.YK_AUTH)) {
                        ToastMaker.showToastShort("登录失败");
                        return;
                    }

                    AppPreferences.putString("token", loginResult.data.YK_AUTH);
                    AppPreferences.putString(Constants.USER_ID, name);
//                    CaseActivity.showActivity(LoginActivity.this, CaseActivity.class);
                    MainActivity.showActivity(LoginActivity.this, MainActivity.class);
                    finish();
                }else{
                    ToastMaker.showToastShort("登录失败");
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                hideLoadingDialog();
            }
        });
    }
}
